﻿using System;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using System.Web.Configuration;

/// <summary>
/// Affair 的摘要说明
/// </summary>
public class Affair
{
    public MySqlConnection mysqlcon;
    public Affair()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
        try
        {
            string constring = WebConfigurationManager.ConnectionStrings["mycon"].ConnectionString;
            mysqlcon = new MySqlConnection(constring);
        }
        catch (Exception e)
        {

            System.Web.HttpContext.Current.Response.Write(e.Message);
        }
    }
    /// <summary>
    /// 执行事务
    /// </summary>
    /// <param name="sqlList">SQL语句列表</param>
    /// <returns></returns>
    public bool ExecuteTransaction()
    {
        string[] temArr = { "SELECT SQL_CALC_FOUND_ROWS *FROM `admin` limit 0, 5;", "SELECT FOUND_ROWS();"};
        List<string> SqlList = new List<string>(temArr);
        bool rst = true;
        mysqlcon.Open();
        //...初始化连接   
        //   开启事务  
        MySqlTransaction sqlTransaction = mysqlcon.BeginTransaction();
        //   将事务应用于Command   
        MySqlCommand sqlCommand = new MySqlCommand();
        sqlCommand.Connection = mysqlcon;
        sqlCommand.Transaction = sqlTransaction;
        try
        {
            //   利用sqlcommand进行数据操作   
            //...   

            foreach (string strsql in SqlList)
            {
                sqlCommand.CommandText = strsql;
                sqlCommand.ExecuteScalar();
            }
            //   成功提交  
            sqlTransaction.Commit();
        }
        catch (Exception ex)
        {
            //   出错回滚   
            rst = false;
            sqlTransaction.Rollback();
        }
        finally
        {
            mysqlcon.Close();
            sqlTransaction.Dispose();
            mysqlcon.Dispose();
        }
        return rst;
    }

}